/**
 * Copyright  (c) 2009-2010 Wilmar IT Service,  Inc.   
 * All  rights  reserved. 
 */
package com.wilmar.process.dao.procedure.impl;

import java.sql.Connection;
import java.sql.SQLException;

import oracle.jdbc.pool.OracleConnectionPoolDataSource;

import org.hibernate.cfg.Configuration;

import com.wilmar.process.core.ProcessSystemDeclareKeys;
import com.wilmar.process.dao.procedure.api.IBaseProcedureProxy;
import com.wilmar.process.enumerate.EnumProcedureName;

/**
 * @author MaKuBEX Email:makubexnet@gmail.com
 * @version 1.0.0 Apr 21, 2010
 * @since 1.0.0
 * 
 */
public abstract class BaseProcedureProxyImpl<T> implements
		IBaseProcedureProxy<T> {

	private static final String HIBERNATE_PROPERTY_CONNECTION_URL = "hibernate.connection.url";
	private static final String HIBERNATE_PROPERTY_CONNECTION_USERNAME = "hibernate.connection.username";
	private static final String HIBERNATE_PROPERTY_CONNECTION_PASSWORD = "hibernate.connection.password";

	private String configFile;
	protected Configuration hibernateConfig;

	/**
	 * 根据hibernate配置文件获取jdbc连接
	 * 
	 * @return jdbc连接
	 */
	protected Connection getConnectioin() {
		if (hibernateConfig == null)
			hibernateConfig = new Configuration().configure(this.configFile);
		String oracle_url = hibernateConfig
				.getProperty(HIBERNATE_PROPERTY_CONNECTION_URL);
		String oracle_user = hibernateConfig
				.getProperty(HIBERNATE_PROPERTY_CONNECTION_USERNAME);
		String oracle_pwd = hibernateConfig
				.getProperty(HIBERNATE_PROPERTY_CONNECTION_PASSWORD);
		OracleConnectionPoolDataSource ocps = null;
		Connection conn = null;
		try {
			ocps = new OracleConnectionPoolDataSource();
			ocps.setURL(oracle_url);
			ocps.setUser(oracle_user);
			ocps.setPassword(oracle_pwd);
			conn = ocps.getConnection();
			conn.setAutoCommit(false);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	/**
	 * 生成存储过程调用的语句
	 * 
	 * @param procedureName
	 *            存储过程名称
	 * @param paramCount
	 *            参数个数
	 * @return 存储过程调用语句
	 */
	protected String generateProcedureCallStatement(
			EnumProcedureName procedureName, int paramCount) {
		StringBuffer proc = new StringBuffer();
		proc.append(ProcessSystemDeclareKeys.SQL_STATEMENT_LEFT_BRACE);
		proc.append(ProcessSystemDeclareKeys.SQL_STATEMENT_CALL);
		proc.append(procedureName);
		proc.append(ProcessSystemDeclareKeys.LEFT_PARENTHESES);
		for (int index = 1; index <= paramCount; index++) {
			proc.append("?");
			if (index != paramCount) {
				proc.append(ProcessSystemDeclareKeys.COMMA);
			}
		}
		proc.append(ProcessSystemDeclareKeys.RIGHT_PARENTHESES);
		proc.append(ProcessSystemDeclareKeys.SQL_STATEMENT_RIGHT_BRACE);
		return proc.toString();
	}

	/**
	 * @return the configFile
	 */
	public String getConfigFile() {
		return configFile;
	}

	/**
	 * @param configFile
	 *            the configFile to set
	 */
	public void setConfigFile(String configFile) {
		this.configFile = configFile;
	}
}
